package javamobile.billsplit;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javamobile.utils.MySocket;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.*;
import android.view.View.*;
import android.widget.*;

public class MainActivity extends Activity{
	
	public static final String PREFS_NAME = "MyPrefsFile";

	private Button signin;
	private Button register;
	private EditText password;
	private EditText username;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        signin = (Button) findViewById(R.id.signin_MAIN);
        register = (Button) findViewById(R.id.register_MAIN);
        password = (EditText) findViewById(R.id.password_MAIN);
        username = (EditText) findViewById(R.id.username_MAIN);
        register.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View v) {
        		Intent toRegister = new Intent(MainActivity.this, RegisterActivity.class);
        		startActivity(toRegister);
        	}
        });
        
        signin.setOnClickListener(new OnClickListener() {
        	@SuppressWarnings("unchecked")
			@Override
        	public void onClick(View v) {
        		Intent toHome = new Intent(MainActivity.this, HomeActivity.class);
        	    Map<String, Object> input = new HashMap<String, Object>();
                String password_str = password.getText().toString();
                String username_str = username.getText().toString();
        		input.put("username", username_str);
        		input.put("password", password_str);
        		input.put("action", "login.do");
        		try {
        			Map<String, Object> output = MySocket.runSocket(input);
	        	    List<String> errors = (List<String>) output.get("errors");
	        	    if (errors.size()==0) {
	        	    	startActivity(toHome);
	        	    	SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
	        	    	SharedPreferences.Editor editor = settings.edit();
	        	    	editor.putString("username", username_str);
	        	    	editor.commit();
	        	    	finish();
	        	    }
	        	    else {
	        	    	Intent toError = new Intent(MainActivity.this, ErrorActivity.class);
	        	    	toError.putStringArrayListExtra("errorlist", new ArrayList<String>(errors));
	        	    	startActivity(toError);
	        	    }
				} catch (UnknownHostException e) {
					System.out.println("UnknownHostException");
					e.printStackTrace();
				} catch (IOException e) {
					System.out.println("IOException");
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					System.out.println("ClassNotFoundException");
					e.printStackTrace();
				}        	
        	}
        });
    }
    
    // need to do the menu thing
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {
       String menu_str = item.getTitle().toString();
       if (menu_str.equals("Home")) {
    	   Intent toHome = new Intent(MainActivity.this, HomeActivity.class);
    	   startActivity(toHome);
       }
       return super.onOptionsItemSelected(item); 
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true; 
    }
}
